//
//  ForMyCommentCtl.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/4/20.
//  Copyright © 2016年 game. All rights reserved.
//

#import "ForMyCommentCtl.h"
#import "ForMyCommentCell.h"
#import "TextPutView.h"
#import "ForCommentModel.h"
#import "SpacePagerModel.h"
#import "SeleCtContrl.h"
#import "YYText.h"
#import "OtherInformationCtrl.h"
#import "RootTableview.h"
#import "MJRefresh.h"
@interface ForMyCommentCtl ()<UITableViewDataSource,UITableViewDelegate,TextPutViewDeleget,ForMyCommentCellDeleget>

@property (nonatomic,strong) RootTableview *tableview;
@property (nonatomic,assign) CGFloat cellHight;
@property (nonatomic,strong) TextPutView *textPutview;
@property (nonatomic,strong) ForCommentModel *model;
@property (nonatomic,assign) NSInteger nextpage;
@property (nonatomic,strong) NSMutableArray *dataAry;
/**
 *  点击cell上的回复  该条评论的ID和评论所属文章的ID和板块ID
 */
@property (nonatomic,copy) NSString *tid;
@property (nonatomic,copy) NSString *pid;
@property (nonatomic,copy) NSString *fid;
@property (nonatomic,assign) CGFloat cellhight;
@property (nonatomic,assign) BOOL IsRefresh;
@property (nonatomic,strong) NSMutableDictionary *cellHightDic;
@end

@implementation ForMyCommentCtl
-(NSMutableDictionary *)cellHightDic{
    if (!_cellHightDic) {
        _cellHightDic = [NSMutableDictionary dictionary];
    }
    return _cellHightDic;
}

-(NSMutableArray *)dataAry{
    if (!_dataAry) {
        _dataAry = [NSMutableArray array];
    }
    return _dataAry;
}
-(TextPutView *)textPutview{
    if (!_textPutview) {
        _textPutview = [[TextPutView alloc]initWithFrame:self.view.bounds];
        _textPutview.alpha = 0;
        _textPutview.deleget = self;
        [self.view addSubview:_textPutview];
        [self.view sendSubviewToBack:_textPutview];
    }
    

    return _textPutview;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.app.myTabBar.tabBar.hidden = YES;
    self.navigationController.navigationBar.hidden = YES;
    StatusBlack;
}
- (IBAction)backAction:(id)sender {
    
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.tableview = [[RootTableview alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT-64) style:UITableViewStylePlain];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    
    [self.tableview registerNib:[UINib nibWithNibName:@"ForMyCommentCell" bundle:nil] forCellReuseIdentifier:@"forcell"];
    [self.view addSubview:self.tableview];
    self.tableview.separatorStyle = 0;
    self.tableview.backgroundColor = tabPlainBackColor;
    
    self.tableview.footer.automaticallyHidden = YES;
   
    self.tableview.footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(RefreshFooter)];
    
    self.nextpage = 2;
    
    [self AchiveDataWhithPage:1];

    __weak typeof(self) weakself = self;
    _tableview.TapBlok = ^{ [weakself AchiveDataWhithPage:1]; };

}

-(void)RefreshFooter{
    
    SpacePagerModel *pageModel = self.model.pager;
    if (self.nextpage >pageModel.total_page.integerValue) {
        [self.tableview.footer noticeNoMoreData];
    }else
        [self AchiveDataWhithPage:self.nextpage];
    self.nextpage ++;
}


-(void)AchiveDataWhithPage:(NSInteger)page{
    
    [[Manager sharedManager]RequestWhithURL:[NSString stringWithFormat:MyComment,(long)page] AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
        
        if (error) {
            self.tableview.isShowFile  = YES;
            [self.tableview reloadData];
            [_tableview.footer endRefreshing];
        }else{
        self.model = [[ForCommentModel alloc]initWithDictionary:responseObject[@"data"] error:nil];
        [self.dataAry addObjectsFromArray:self.model.list];
        
        self.tableview.isShowImage = YES;
        [self.tableview reloadData];
        [_tableview.footer endRefreshing];
        }
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.dataAry.count;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{

    return  1+[[self.cellHightDic valueForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]]floatValue];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    ForMyCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:@"forcell"];
    cell.model = self.dataAry[indexPath.row];
    cell.deleget = self;

    [self.cellHightDic setValue:[NSString stringWithFormat:@"%f",cell.size.height] forKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]];
    return cell;
}

#pragma mark---cell中的代理方法  点击头像用户名查看个人空间
-(void)SpaceActionWhithId:(NSString *)uid{
    OtherInformationCtrl *space = [OtherInformationCtrl new];
    space.uid = uid;
    [self.navigationController pushViewController:space animated:YES];

}
#pragma mark---cell中的代理方法 ，点击回复 回复当前评论
-(void)ReplayActionWhithTid:(NSString *)tid AndPid:(NSString *)pid AndFid:(NSString *)fid AndText:(NSString *)text{
    self.tid = tid;
    self.pid = pid;
    self.fid = fid;
    self.textPutview.alpha = 1;
    _textPutview.inputBar.placeHolder = text;
    [_textPutview.inputBar.textView becomeFirstResponder];
    [self.view bringSubviewToFront:self.textPutview];
}

#pragma mark-- TextPutViewDeleget 点击发送
-(void)DidSender:(NSString *)sendText{
    
    [[Manager sharedManager]RequestWhithURL:GetFormhash AndParameter:nil AndCompletionType:^(id responseObject, DIEError *error) {
        if ([responseObject[@"status"] isEqualToString:@"SUCCESS"]) {
            
            NSDictionary *dic = @{
                                  @"fid":self.fid,
                                  @"tid":self.tid,
                                  @"message":sendText,
                                  @"formhash":responseObject[@"data"][@"formhash"],
                                  @"replysubmit":@"yes",
                                  @"repquote":self.pid,
                                  };
            [self replayPostWhithDic:dic];
        }
    }];
}
-(void)replayPostWhithDic:(NSDictionary *)dic{
    
    [[Manager sharedManager]ResponseWhithURL:CommentUrl AndParameter:dic AndCompletionType:^(id responseObject, DIEError *error) {
        
        if (![responseObject[@"status"] isEqualToString:@"SUCCESS"]) {
            [Manager GetHudWhithText:@"回复失败" AndFatherView:self.view];
        }else{
            [Manager GetHudWhithText:@"回复成功" AndFatherView:self.view];
        }
    }];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

    ForMyCommentCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    SeleCtContrl *str = [SeleCtContrl new];
    str.tid = cell.tid;
    str.url = [NSString stringWithFormat:MainSelet,cell.tid];
    str.rowOfCell = indexPath.row;
    [self.navigationController pushViewController:str animated:YES];
}


@end